package top.infopub.mgr.util;


import java.util.Iterator;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import top.infopub.model.sys.ParameterGroupDetail;
import top.infopub.tdp.util.GrpParaUtil;


/**
 * 数据字典工具类
 * @author Awoke
 * @version 2018年10月15日
 * @see SysParaUtil
 * @since
 */
public final class SysParaUtil {

    /**
     * 系统参数CODE
     */
    private static final String SYS_PARAM_CODE = "A00001025";

    /**
     * 根据参数名字获取[系统参数A00001025]配置值
     * @param paramName
     * @return
     */
    public static String getSysParameter(String paramName) {
        Set<ParameterGroupDetail> detailList = GrpParaUtil.get(SYS_PARAM_CODE);
        Iterator<ParameterGroupDetail> iterator = detailList.iterator();
        while (iterator.hasNext()) {
            ParameterGroupDetail detail = iterator.next();
            if (detail.getSname().equals(paramName)) {
                return detail.getSvalue();
            }
        }
        return null;
    }

    /**
     * 
     * @param groupCode
     * @param entire 是否显示全部
     * @return
     */
    public static String getGrpParamStr(String groupCode, String entire, String dataStr) {
        Set<ParameterGroupDetail> detailList = GrpParaUtil.get(groupCode);;
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        int size = (detailList != null ? detailList.size() : 0);
        if (entire.equalsIgnoreCase("true")) {
            if (size > 0) {
                if (StringUtils.isBlank(dataStr)) {
                    sb.append("[\"\",\"全部\"],");
                }
                else {
                    sb.append(dataStr + ",");
                }
            }
        }
        if (detailList != null && size > 0) {
            int i = 0;
            Iterator<ParameterGroupDetail> iterator = detailList.iterator();
            while (iterator.hasNext()) {
                ParameterGroupDetail detail = iterator.next();
                if (i == size - 1) {
                    sb.append("[\"" + detail.getSvalue() + "\",\"" + detail.getSname() + "\"]");
                }
                else {
                    sb.append("[\"" + detail.getSvalue() + "\",\"" + detail.getSname() + "\"],");
                }
                i++ ;
            }
        }
        sb.append("]");
        return sb.toString();
    }

}
